package com.example.day03.lianxi.wordtext;


import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.operators.UnsortedGrouping;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * 批处理，不被推荐
 */
public class Demo1 {

    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> ds = env.readTextFile("E:\\note\\flink-project\\flink-leaning\\day04\\input\\word.txt");

        FlatMapOperator<String, Tuple2<String, Integer>> stringTuple2FlatMapOperator = ds.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {

            @Override
            public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
                String[] split = s.split(" ");
                for (String s1 : split) {
                    Tuple2<String, Integer> stringIntegerTuple2 = Tuple2.of(s1, 1);
                    collector.collect(stringIntegerTuple2);
                }
            }
        });


        UnsortedGrouping<Tuple2<String, Integer>> tuple2UnsortedGrouping = stringTuple2FlatMapOperator.groupBy(0);
        tuple2UnsortedGrouping.sum(1).print();

        env.execute();



    }
}
